/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewAuthorPanel.java
 *
 * Created on 26-dic-2013, 18.23.43
 */

package APP2.bookshop.GUI;

import APP2.bookshop.author.Author;
import APP2.bookshop.main.BookShop;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;

/**
 *
 * @author finalfire
 */
public class ViewAuthorPanel extends javax.swing.JPanel {

    BookShop bookshop;

    /** Creates new form ViewAuthorPanel */
    public ViewAuthorPanel(BookShop bookshop) {
        this.bookshop = bookshop;
        initComponents();
    }

    private void updateLists() {
        authorsList.setModel(this.bookshop.getAuthorController().getAuthorsModel());
        DefaultListModel list = new DefaultListModel();
        booksWrittenByList.setModel(list);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane2 = new javax.swing.JScrollPane();
        booksWrittenByList = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        authorsList = new javax.swing.JList();
        deleteAuthorButton = new javax.swing.JButton();

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jLabel3.setText("View Authors");

        jScrollPane2.setViewportView(booksWrittenByList);

        jLabel5.setForeground(new java.awt.Color(51, 51, 51));
        jLabel5.setText("Click on the author's name to see the related books.");

        authorsList.setModel(this.bookshop.getAuthorController().getAuthorsModel());
        authorsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                authorsListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(authorsList);

        deleteAuthorButton.setText("Delete author");
        deleteAuthorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteAuthorButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                    .add(jLabel3)
                    .add(jLabel5)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 236, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, deleteAuthorButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel5)
                .add(18, 18, 18)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 157, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(9, 9, 9)
                .add(deleteAuthorButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void deleteAuthorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteAuthorButtonActionPerformed
        if (this.authorsList.getSelectedIndex() != -1) {
            int choice = JOptionPane.showConfirmDialog(this,
                    "Are you sure to delete this author?", "Delete author",
                    JOptionPane.YES_NO_OPTION);

            if (choice == JOptionPane.YES_OPTION)
                if(this.bookshop.getAuthorController().deleteAuthor(this.bookshop.getAuthorController().getAuthorByName(this.authorsList.getSelectedValue().toString()))) {
                    this.updateLists();
                    JOptionPane.showMessageDialog(this, "Author correctly deleted!");
                }
        }
    }//GEN-LAST:event_deleteAuthorButtonActionPerformed

    private void authorsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_authorsListMouseClicked
        // selected an author, retrieve it
        String name = (String) this.authorsList.getSelectedValue();
        System.out.println(name);

        // update the list "books written by" the selected author
        booksWrittenByList.setModel(this.bookshop.getAuthorController().getAllBooksByAuthor(name));
    }//GEN-LAST:event_authorsListMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList authorsList;
    private javax.swing.JList booksWrittenByList;
    private javax.swing.JButton deleteAuthorButton;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

}
